from typing import *

class Solution:
    def subdomainVisits(self, cpdomains: List[str]) -> List[str]:
        domainCounts = {}
        for cpdomain in cpdomains:
            rep_domain = cpdomain.split()
            count = int(rep_domain[0])
            domain = rep_domain[1]
            while True:
                domainCounts[domain] = domainCounts.get(domain, 0) + count
                upperDomain = domain.split('.', 1)
                if len(upperDomain) == 1:
                    break
                else:
                    domain = upperDomain[1]
        result = []
        for domain, count in domainCounts.items():
           result.append(f"{count} {domain}") 
        return result
